//oOQWrjm0uPWLiO4iTGmpe6qNkEbE
import getData from '../../service/getData.js'
import * as types from '../types'

export const center = {
    state:{
        /**
         * 每个模块包括 列表、分页、每页显示条数
         */
//  我的电子券
        myCoupon:[],
        couponsPage:1,
        couponsPageLimit:10,
//  我的收支明细
        paymentList:[],
        paymentPage:1,
        paymentPageLimit:10,
        myAccount:[],//我的账户
//  我的钱包明细
        myWalletList:[],
        myWalletPage:1,
        myWalletPageLimit:10,
        myWallet:'',//我的钱包
//  我的评论
        commentList:[],
        replyList:[],
        commentsPage:1,
        commentsPageLimit:10,
        comment:[],//评论总统计
// 电子券交易
        couponDealList:[],
        couponDealPage:1,
        couponDealLimit:5,
        couponDeal:[],//交易统计
//  我的订单
        myOrderList:[],
        myOrderPage:1,
        myOrderPageLimit:6,
        myOrder:[],
//  我的券友
        friends:[],
        searchFriend:{},
        //评论图片
        commentImg:[]

    },
    actions:{
        //我的电子券
        getMyCoupons:function({ state, commit, rootState,getters }, params){
            let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.couponsPage+"}," +
            "{\"name\":\"iDisplayLength\",\"value\":"+state.couponsPageLimit+"}]"
            };

            params.data = params.data || {};
            params.data = Object.assign(page,params.data);
            return new Promise((resolve, reject) => {
                if(rootState.coupon.scroll) {
                    commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                    getData.myCoupons(params, getters.userNum).then(function (rep) {
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                        resolve(rep);
                        commit(types.GET_MY_COUPONS, rep);
                        if(rep.content.couponList.length == 0) {
                            return;
                        }
                        state.couponsPage++;
                    }).catch(rep => {
                        reject(rep);
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                    })
                }
            });
        },
        //我的账户总金额
        getAccount:function({ state, commit, rootState,getters }, params){

            getData.myAccount({},getters.userNum).then(function(rep){
                if(rep.respCode == '00'){
                    commit(types.GET_USER_ACCOUNT,rep.content)
                }
            })
        },
        //我的收支明细
        getPaymentList:function({ state, commit, rootState,getters }, params){

            let page = {
                "aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.paymentPage+"}," +
                "{\"name\":\"iDisplayLength\",\"value\":"+state.paymentPageLimit+"}]"
            };
            params.data = Object.assign(page,params.data);
            return new Promise((resolve, reject) => {
                if(rootState.coupon.scroll) {
                    commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                    getData.paymentList(params, getters.userNum).then(function (rep) {
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                        resolve(rep);

                        commit(types.GET_PAYMENTLIST,rep);
                        if(rep.content.list.length == 0) {
                            return;
                        }

                    }).catch(function(rep){
                        reject(rep)
                    })
                }
            });
        },
        //我的钱包总金额
        getMyWallet:function({state, commit, rootState,getters}, {params, usrId}){
            return new Promise((resolve, reject) => {
                    getData.myWallet(params, usrId).then(function (rep) {
                        if(rep.respCode == '00'){
                            commit(types.GET_USER_WALLET,rep.content)
                        }
                        resolve(rep);
                    }).catch(function(rep){
                        reject(rep)
                    })
            });

        },
        //我的钱包明细
        getMyWalletList:function({ state, commit, rootState,getters }, params){
            let page = {
                "aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.myWalletPage+"}," +
                "{\"name\":\"iDisplayLength\",\"value\":"+state.myWalletPageLimit+"}]"
            };
            let type = params.data.queryType
            params.data = Object.assign(page, params.data);
            if (type == 1) {
              return new Promise((resolve, reject) => {
                if(rootState.coupon.scroll) {
                  getData.myRewards(params).then(function (rep) {
                    commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                    resolve(rep);
                    commit(types.GET_MYWALLETLIST,rep);
                    if(rep.content.list.length == 0) {
                      return;
                    }
                  }).catch(function(rep){
                    reject(rep)
                  })
                }
              });
            } else {
              return new Promise((resolve, reject) => {
                // if(rootState.coupon.scroll) {
                  commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                  getData.myWalletList(params).then(function (rep) {
                    commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                    resolve(rep);

                    commit(types.GET_MYWALLETLIST,rep);
                    if(rep.content.list.length == 0) {
                      return;
                    }
                  }).catch(function(rep){
                    reject(rep)
                  })
                // }
              });
            }
        },


        //我的评论
        getMyComments:function({state, commit, rootState,getters}, params){
            let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.commentsPage+"}," +
            "{\"name\":\"iDisplayLength\",\"value\":"+state.commentsPageLimit+"}]"
            };
            params.data = params.data || {};
            params.data = Object.assign(page,params.data);

            return new Promise((resolve, reject) => {

                    commit(types.GET_SCORLL_STATUS,false);//避免快速滚动

                    getData.myComments(params, getters.userNum).then(function (rep) {
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                        resolve(rep);

                        if(rep.respCode == "00"){

                            commit(types.MY_COMMENTS,rep);

                            if(rep.content.usrCommentList.length == 0) {
                                return;
                            }

                            state.commentsPage++;
                        }
                    }).catch(function(rep){
                        reject(rep)
                    })

            });
        },
        //电子券交易
        couponDealList:function({ state, commit, rootState,getters}, params){
            let page = {
                "aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.couponDealPage+"}," +
                "{\"name\":\"iDisplayLength\",\"value\":"+state.couponDealLimit+"}]"
            };
            params.data = Object.assign(page,params.data);
            return new Promise((resolve, reject) => {
                if(rootState.coupon.scroll) {
                    commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                    getData.couponDealList(params, getters.userNum).then(function (rep) {
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                        resolve(rep);
                        if(rep.respCode == "00"){
                            commit(types.COUPONS_DEAL,rep);
                            if(rep.content.list.length == 0) {
                                return;
                            }
                            state.couponDealPage++;
                        }
                    }).catch(function(rep){
                        reject(rep)
                    })
                }
            });
        },
        //我的订单
        myOrder:function({ state, commit, rootState,getters}, params){
            let page = {
                "aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.myOrderPage+"}," +
                "{\"name\":\"iDisplayLength\",\"value\":"+state.myOrderPageLimit+"}]"
            };
            params.data = Object.assign(page,params.data);
            return new Promise((resolve, reject) => {
                if(rootState.coupon.scroll) {
                    commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                    getData.myOrder(params, getters.userNum).then(function (rep) {
                        commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                        resolve(rep);
                        if(rep.respCode == "00"){
                            commit(types.MYORDER,rep);
                            if(rep.content.orderList.length == 0) {
                                return;
                            }
                            state.myOrderPage++;
                        }
                    }).catch(function(rep){
                        reject(rep)
                    });
                }
            });
        },
    },
    getters:{
        userNum:function(state, getters, rootState){
            return rootState.user.userInfo.usrId;
        },
        walletAcc:function (state, getters, rootState) {
            return 'U'+rootState.user.userInfo.usrId;
        }
    },
    mutations:{
        /**
         *我的电子券
         */
            //列表
            [types.GET_MY_COUPONS](state,rep){
            if(!rep){
                state.myCoupon = [];
                return
            }
            if(rep.content.couponList.length == 0){
                return;
            }
            if (state.couponsPage <= 1) {
                state.myCoupon = rep.content.couponList
            } else {
                state.myCoupon = state.myCoupon.concat(rep.content.couponList)
            }
        },
        //分页
        [types.CHANGE_COUPON_PAGE](state,num){
            state.couponsPage = num;
        },
        /**
         *我的收支明细
         */
            //列表
            [types.GET_PAYMENTLIST](state,rep){
            if(!rep){
                state.paymentList = [];
                return;
            }
            if(rep.content.list.length == 0){
                return;
            }
            for (var i = rep.content.list.length - 1; i >= 0; i--) {
                var index;
                if (rep.content.list[i].content) {
                    if (rep.content.list[i].content.indexOf('折扣券')>=0) {
                        index=rep.content.list[i].content.indexOf('折扣券');
                        rep.content.list[i].content=rep.content.list[i].content.substring(0,index)+'<span style="color:#77bc6b">折扣券</span>'+rep.content.list[i].content.substring(index+3)
                    }else if(rep.content.list[i].content.indexOf('代金券')>=0){
                        index=rep.content.list[i].content.indexOf('代金券');
                        rep.content.list[i].content=rep.content.list[i].content.substring(0,index)+'<span style="color:#f59e06">代金券</span>'+rep.content.list[i].content.substring(index+3)
                    }else if(rep.content.list[i].content.indexOf('抵用券')>=0){
                        index=rep.content.list[i].content.indexOf('抵用券');
                        rep.content.list[i].content=rep.content.list[i].content.substring(0,index)+'<span style="color:#72afc4">抵用券</span>'+rep.content.list[i].content.substring(index+3)
                    }else if(rep.content.list[i].content.indexOf('礼品券')>=0){
                        index=rep.content.list[i].content.indexOf('礼品券');
                        rep.content.list[i].content=rep.content.list[i].content.substring(0,index)+'<span style="color:#fe7e5c">礼品券</span>'+rep.content.list[i].content.substring(index+3)
                    }else if(rep.content.list[i].content.indexOf('票务')>=0){
                        index=rep.content.list[i].content.indexOf('票务');
                        rep.content.list[i].content=rep.content.list[i].content.substring(0,index)+'<span style="color:#b790cd">票务</span>'+rep.content.list[i].content.substring(index+3)
                    }
                }

            };
            if (state.paymentPage <= 1) {
                state.paymentList = rep.content.list
            } else {
                state.paymentList = state.paymentList.concat(rep.content.list)
            }
            state.paymentPage++;
        },
        //tab + 分页
        [types.PAYMENT_PAGE](state,num){
            state.paymentPage = num;
        },
        [types.GET_USER_ACCOUNT](state,num){
            state.myAccount = num
        },


        /**
         *我的钱包明细
         */
        //列表
            [types.GET_MYWALLETLIST](state,rep){
            if(!rep){
                state.myWalletList = [];
                return;
            }
            if(rep.content.list.length == 0){
                return;
            }
            if (state.myWalletPage <= 1) {
                state.myWalletList = rep.content.list
            } else {
                state.myWalletList = state.myWalletList.concat(rep.content.list)
            }
            state.myWalletPage++;
        },
        //tab + 分页
        [types.MYWALLET_PAGE](state,num){
            state.myWalletPage = num;
        },
        [types.GET_USER_WALLET](state,num){
            state.myWallet = num.amt;
        },

        /**
         *我的评论
         */
        //列表
        [types.MY_COMMENTS](state,rep){
            if(!rep){
                state.commentList = [];
                state.comment = [];
                return
            }

            state.comment = rep.content;
            if(rep.content.usrCommentList.length == 0){
                return;
            };
            var imgStrs= [];

            for (var i = rep.content.usrCommentList.length - 1; i >= 0; i--) {
                if(rep.content.usrCommentList[i].picUrl){
                    imgStrs=rep.content.usrCommentList[i].picUrl.split(",");
                    imgStrs.length=imgStrs.length-1;
                    rep.content.usrCommentList[i].picUrl=imgStrs;
                }
                if (rep.content.usrCommentList[i].dsc) {
                    if (rep.content.usrCommentList[i].dsc.length>3) {
                        rep.content.usrCommentList[i].dsc.length=3;
                    }
                }

            }

            if (state.commentsPage <= 1) {
                state.commentList = rep.content.usrCommentList
            } else {
                state.commentList = state.commentList.concat(rep.content.usrCommentList)
            }

        },
        [types.MY_COMMENTS_PAGE](state,num){
            state.commentsPage = num;
        },
        //分页
        /**
         *电子券交易
         */
            //列表
            [types.COUPONS_DEAL](state,rep){
            if(!rep){
                state.couponDealList = [];
                return
            }
            state.couponDeal = rep.content;
            if(rep.content.list.length == 0){
                return;
            }
            if (state.couponDealPage <= 1) {
                state.couponDealList = rep.content.list
            } else {
                state.couponDealList = state.couponDealList.concat(rep.content.list)
            }
        },
        //分页
        [types.COUPONS_PAGE](state,num){
            state.couponDealPage = num;
        },
        /**
         *我的订单
         */
            //列表
            [types.MYORDER](state,rep){
            if(!rep){
                state.myOrderList = [];
                state.myOrder = [];
                return
            }
            state.myOrder = rep.content;
            if(rep.content.orderList.length == 0){
                return;
            }
            if (state.myOrderPage <= 1) {
                state.myOrderList = rep.content.orderList
            } else {
                state.myOrderList = state.myOrderList.concat(rep.content.orderList)
            }
        },
        //分页
        [types.MYORDER_PAGE](state,num){
            state.myOrderPage = num;
        },
    /**
     * 我的券友
     */
      [types.search_friend](state,param){
        state.searchFriend = param;
      }
    }
}
